import com.mysql.jdbc.jdbc2.optional.MysqlDataSource;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestJDBCSelect {
    public static void main(String[] args) throws SQLException {
        //1.构造数据源
        DataSource dataSource = new MysqlDataSource();
        ((MysqlDataSource)dataSource).setUrl("jdbc:mysql://127.0.0.1:3306/jdbc?useSSL=false");
        ((MysqlDataSource)dataSource).setUser("root");
        ((MysqlDataSource)dataSource).setPassword("080912");
        //2.建立连接
        Connection connection = dataSource.getConnection();
        //3.构造sql语句
        String sql = "select * from student";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);
        //4.执行sql
        ResultSet resultSet = preparedStatement.executeQuery();
        while(resultSet.next()){
            //获取id列的数据
            int id = resultSet.getInt("id");
            //获取name这一列的数值
            String name = resultSet.getNString("name");
            System.out.print("id:"+id);
            System.out.println(" name:"+name);

        }
        resultSet.close();
        preparedStatement.close();
        connection.close();


    }
}
